<template>
  <div id="box">

    <!-- 信息 -->
    <div id="msg">
      <span>{{article.createTime}}</span>
      <span>{{article.author}}</span>
      <br />
      <span>labal:</span>
      <span v-for="tag,index in article.tags" :key="index">
        {{tag}}
      </span>
    </div>

    <div id="container">
      <!-- title -->
      <div id="title">{{article.title}}</div>
      <!-- 主要内容 -->
      <div id="content" v-html="article.content"></div>
    </div>

    <hr />

    <div>
      <comment-item v-for="item,index in comments" :key="index" :commentItem="item"></comment-item>
    </div>

    <div class="centerE">
      <el-button size="mini" v-if="hasNext==1" style="margin-top: 0.5rem;" @click="loadMore">加载更多</el-button>
      <span v-else-if="hasNext==0">没有更多了~</span>
      <span v-else="hasNext==-1">暂无评论~</span>
    </div>

    <br />
    <div class="centerE">
      <div v-if="!isLogined">
        <span>请先登录</span>
        <el-button size="mini" @click="login">登录</el-button>
        <el-button size="mini" @click="register">注册</el-button>
      </div>
      <div v-else>
        <span>欢迎，{{this.$store.state.user.nickName}}</span>
        <el-button @click="logout" size="mini">退出</el-button>
      </div>
    </div>
    <editor @submitCommentFromEditor="handlerChildFun"></editor>

  </div>
</template>

<script>
  import editor from '@/components/editor.vue'
  import commentItem from '@/components/comment-item.vue'
  import {
    listCommentsOfArticle,
    listMoreComment
  } from '@/api/Comment.js'
  import {
    getArticleById
  } from '@/api/Article.js'

  export default {
    data() {
      return {
        hasNext: 1,
        minId: 0,
        article: {},
        numOfPage: 3,
        comments: []
      }
    },

    methods: {
      loadMore() {
        var that = this
        listMoreComment(this.article.id, this.minId, this.numOfPage).then(resp => {
          if (resp.data.length == 0) {
            that.hasNext = 0
            return
          }
          that.comments = that.comments.concat(resp.data)
          that.minId = resp.data[resp.data.length - 1].id
        })
      },

      login() {
        this.$router.push({
          path: '/login',
          query: {
            redirect: this.$route.path
          }
        })
      },

      logout() {
        this.$store.commit({
          type: 'setUser',
          value: null
        })
      },

      register() {
        this.$router.push({
          path: '/register',
          query: {
            redirect: this.$route.path
          }
        })
      },

      handlerChildFun(msg) {
        this.comments.unshift(msg.data)
      }
    },

    components: {
      'comment-item': commentItem,
      'editor': editor
    },

    mounted() {
      var that = this
      var articleId = this.$route.params.articleId

      if (that.$store.state.article == null) {
        getArticleById(articleId).then(resp => {
          that.article = resp.data
          that.$store.commit({
            type: 'set',
            value: resp.data
          })
        })
      } else if (articleId == this.$store.state.article.id) {
        this.article = this.$store.state.article
      }

      listCommentsOfArticle(articleId, this.numOfPage).then(resp => {
        that.comments = resp.data
        if (resp.data.length == 0) {
          that.hasNext = -1
          return
        }
        that.minId = resp.data[resp.data.length - 1].id
      }).catch(error => {
        console.log(error)
        that.$router.push('/error')
      })
    },

    computed: {
      isLogined() {
        if (this.$store.state.user == null) {
          return false
        } else {
          return true
        }
      }
    },

    watch: {
      comments: function(newValue, oldValue) {
        if (this.comments.size != 0) {
          this.hasNext = 0;
        }
      }
    },

    beforeDestroy() {
      this.$store.state.article = null
    }
  }
</script>

<style scoped="scoped">
  #box {
    margin-left: 10%;
  }

  #msg {
    background-color: rgba(241, 241, 241, 0.5);
    width: 80%;
    margin-left: 8%;
    border-radius: 10px;
    text-align: left;
    font-size: 14px;
    /* 字间距 */
    word-spacing: 10px;
    /* 行距 */
    line-height: 30px;
    padding-left: 2%;
    padding-right: 2%;
  }

  span {
    margin-left: 5px;
  }

  #container {
    width: 80%;
    /* border: 1px solid #000000; */
    margin-left: 8%;
    margin-top: 10px;
    /* 同色渐变，只求透明 */
    background: linear-gradient(rgba(241, 241, 241, 0.5), rgba(241, 241, 241, 0.1));
    border-radius: 10px;
    padding-left: 2%;
    padding-right: 2%;
    padding-top: 10px;
    padding-bottom: 10px;
  }

  #title {
    margin-top: 10px;
    font-size: 20px;
  }

  #content {
    margin-top: 20px;
    font-size: 14px;
    /* 字间距 */
    word-spacing: 10px;
    /* 行距 */
    line-height: 20px;
    text-align: left;
    font-weight: 400;
  }

  #commentDiv {
    background-color: rgba(241, 241, 241, 0.5);
    width: 70%;
    margin-left: 8%;
    margin-top: 10px;
    border-radius: 10px;
    text-align: left;
    font-size: 14px;
    /* 字间距 */
    word-spacing: 10px;
    padding-left: 2%;
    padding-right: 2%;
  }

  .block {
    margin-top: 5px;
  }

  .centerE {
    margin-left: -18%;
  }

</style>
